package com.ktgj.sms.controller;

import com.alibaba.fastjson.JSON;
import com.ktgj.common.core.utils.HttpClientUtil;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.utils.xml.XElement;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.web.page.TableDataInfo;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.common.sms.entity.RecvBFmoThread;
import com.ktgj.common.sms.entity.RecvMoThread;
import com.ktgj.sms.common.BFSmsUtils;
import com.ktgj.sms.common.MWSmsUtils;
import com.ktgj.sms.domain.CSubmitState;
import com.ktgj.sms.domain.Sms;
import com.ktgj.sms.service.ISmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短信Controller
 *
 * @author ruoyi
 * @date 2021-08-11
 */
@ControllerAdvice
@RestController
@RequestMapping("/sms")
public class SmsController extends BaseController {
    @Autowired
    private ISmsService smsService;
    @Autowired
    private BFSmsUtils bfSmsUtils;
    @Autowired
    private MWSmsUtils mwSmsUtils;

    /**
     * 查询短信列表
     */
    @PreAuthorize(hasPermi = "system:sms:list")
    @GetMapping("/list")
    public TableDataInfo list(Sms sms) {
        startPage();
        List<Sms> list = smsService.selectSmsList(sms);
        return getDataTable(list);
    }

    /**
     * 导出短信列表
     */
    @PreAuthorize(hasPermi = "system:sms:export")
    @Log(title = "短信", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Sms sms) throws IOException {
        List<Sms> list = smsService.selectSmsList(sms);
        ExcelUtil<Sms> util = new ExcelUtil<Sms>(Sms.class);
        util.exportExcel(response, list, "短信数据");
    }

    /**
     * 获取短信详细信息
     */
    @PreAuthorize(hasPermi = "system:sms:query")
    @GetMapping(value = "/{smsId}")
    public AjaxResult getInfo(@PathVariable("smsId") Long smsId) {
        return AjaxResult.success(smsService.selectSmsById(smsId));
    }

    /**
     * 新增短信
     */
//    @PreAuthorize(hasPermi = "system:sms:add")
    @Log(title = "短信", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sms sms) {
        return toAjax(smsService.insertSms(sms));
    }

    @PostMapping("/addSms")
    public boolean addSms(@RequestBody Sms sms) {
        if (smsService.insertSms(sms) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改短信
     */
    @PreAuthorize(hasPermi = "system:sms:edit")
    @Log(title = "短信", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sms sms) {
        return toAjax(smsService.updateSms(sms));
    }

    /**
     * 删除短信
     */
    @PreAuthorize(hasPermi = "system:sms:remove")
    @Log(title = "短信", businessType = BusinessType.DELETE)
    @DeleteMapping("/{smsIds}")
    public AjaxResult remove(@PathVariable Long[] smsIds) {
        return toAjax(smsService.deleteSmsByIds(smsIds));
    }


    @PostMapping("/upSms")
    public void getUpSmsMessage() {
        mwSmsUtils.upMWSmsThread();
    }


    @PostMapping("/upBFBsms")
    public void upBaifenSms() {
        bfSmsUtils.upBFSmsThread();
    }

    @PostMapping(value = "/sendSmsMessage")
    public boolean sendSmsMessage(@RequestBody Sms sms) {
        CSubmitState cSubmitState = bfSmsUtils.sendSmsInfo(sms);
        if (cSubmitState.getState().equals("0")) {
            return true;
        } else {
            if (mwSmsUtils.sendSmsInfo(sms)) {
                return true;
            }
        }
        return false;
    }
}
